﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SocketControl.Protocol.Http
{
    public class GetChunkedData : Http
    {
        private int responseOffset = 0;
        private int chunkedOffset = 0;
        private int dataSizeoffset = 0;
        private bool readChunkedRealy = false;

        public EventHandler<Event.HttpEventArgs> GetChunkedSizeRealy;
        public void GetChunkedSize(object sender, byte[] param)
        {
            int offset = this.GetResponseMessageOffset;
            for (offset = 0; offset < this.GetTransferData.Length; offset++)
            {
                #region getChunkSize
                ++chunkedOffset;
                if (offset >= 1)
                {
                    if (param[offset - 1] == 13 && param[offset] == 10)
                    {
                        byte[] sizeBuff = new byte[chunkedOffset - 2];
                        Array.Copy(this.TransferCache.GetBuffer(), this.GetResponseMessageOffset, sizeBuff, 0, chunkedOffset - 2);
                        dataSizeoffset = BitConverter.ToInt32(sizeBuff, 0);
                        readChunkedRealy = true;
                        break;
                    }
                }
            }

            if (readChunkedRealy)
            {
                if (GetChunkedSizeRealy != null)
                {
                    Event.HttpEventArgs args = new Event.HttpEventArgs();
                    byte[] buff = new byte[param.Length - offset];
                    Array.Copy(param, offset, buff, 0, param.Length - offset);
                    args.handle = this;
                    args.param = buff;
                    GetChunkedSizeRealy(this, args);
                    buff = null;
                }
            }

            #endregion
        }

        public void GetBody(byte[] param)
        {
            ;
        }
    }
}
